//
//  HomeBaseViewController.m
//  GiftNote
//
//  Created by lanou on 15/8/12.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "HomeBaseViewController.h"
#import "HomeHeader.h"
#import "HomeGiftCell.h"
#import "LORequestManger.h"
#import "HomeGiftModel.h"
#import "MJRefresh.h"
#import "HomeBaseDetailViewController.h"

typedef NS_ENUM(NSInteger, RefreshDownLoadType) {
    RefreshDownLoadTypeUp,
    RefreshDownLoadTypeDown
};


@interface HomeBaseViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)NSInteger number;
@property (nonatomic, assign)RefreshDownLoadType refreshDownLoadType;
@property (nonatomic, strong)HomeBaseDetailViewController *baseDVC;

@end

@implementation HomeBaseViewController

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    //  改变返回按钮的颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self creatTableView];
    [self upAndDownRefresh];
    
    _refreshDownLoadType = RefreshDownLoadTypeUp;
    _number = 0;
    [self readData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - 创建tableView

- (void)creatTableView
{
//    CGFloat height = self.navigationController.navigationBar.frame.size.height;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight) style:UITableViewStylePlain];
    _tableView.rowHeight = 273 * (kHomeWidth - 20) / 600 + 15;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[HomeGiftCell class] forCellReuseIdentifier:@"HomeGiftCell"];

}


#pragma mark - UITableViewDelegate / UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeGiftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeGiftCell" forIndexPath:indexPath];
    UIView *view = [[UIView alloc]initWithFrame:cell.frame];
    cell.selectedBackgroundView = view;
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.giftModel = _dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeGiftModel *giftModel = _dataSource[indexPath.row];
    
    _baseDVC = [[HomeBaseDetailViewController alloc]init];
    _baseDVC.ID = [NSString stringWithFormat:@"%d",[giftModel.ID intValue]];
    _baseDVC.hidesBottomBarWhenPushed = YES;
    _baseDVC.naVC = _naVC;
    [_naVC pushViewController:_baseDVC animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - 数据解析

- (void)readData
{
    NSString *newUrl = [_url stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)_number]];
    [LORequestManger GET:newUrl success:^(id response) {
        
        if (_refreshDownLoadType == RefreshDownLoadTypeUp) {
            [_dataSource removeAllObjects];
        }
        
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *items = dic[@"data"][@"items"];
        for (NSDictionary *newDic in items) {
            HomeGiftModel *giftModel = [[HomeGiftModel alloc]init];
            [giftModel setValuesForKeysWithDictionary:newDic];
            [self.dataSource addObject:giftModel];
        }
        
        if ([_url isEqualToString:@"http://api.liwushuo.com/v1/collections/22/posts?gender=1&generation=1&limit=20&offset="]) {
            NSArray *posts = dic[@"data"][@"posts"];
            for (NSDictionary *newDic in posts) {
                HomeGiftModel *giftModel = [[HomeGiftModel alloc]init];
                [giftModel setValuesForKeysWithDictionary:newDic];
                [self.dataSource addObject:giftModel];
            }
        }
        
        [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"error:%@",error);
        
    }];
}


- (void)headDownLoad
{
    _number = 0;
    _refreshDownLoadType = RefreshDownLoadTypeUp;
    [self readData];
    [_tableView.header endRefreshing];
}

- (void)footDownLoad
{
    _number += 20;
    _refreshDownLoadType = RefreshDownLoadTypeDown;
    [self readData];
    [_tableView.footer endRefreshing];
}

- (void)setUrl:(NSString *)url
{
    _url = url;
    _number = 0;
    [self readData];
}

- (void)upAndDownRefresh
{
    [_tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(headDownLoad)];
    [_tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(footDownLoad)];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
